package com.commodity.servlet;


import com.commodity.Service.WarehouseService;
import com.commodity.Service.warehouseServiceImp.WarehouseServiceImp;
import com.commodity.entity.Warehouse;
import com.commodity.entity.WarehouseSearchBean;
import com.commodity.utils.BeanFactory;
import com.commodity.utils.PaginateInfo;
import com.commodity.utils.ServletUtils;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import org.springframework.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@WebServlet("/warehouse/*")
public class WarehouseServlet extends HttpServlet {
    private final WarehouseService warehouseService = BeanFactory.getInstance(WarehouseServiceImp.class);
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        String action  = req.getPathInfo();
        String method = req.getMethod().toLowerCase();
        if ("/list".equals(action)){
            if ("get".equals(method)){
                req.getRequestDispatcher("/WEB-INF/jsp/warehouse/list.jsp").forward(req,resp);
            }else {
                list(req,resp);
            }
        }else if("/add".equals(action)){
            if ("get".equals(method)){
                String error = (String)req.getSession().getAttribute("error");
                if (StringUtils.hasText(error)){
                    req.setAttribute("error",error);
                    req.getSession().removeAttribute("error");
                    Warehouse warehouse = (Warehouse) req.getSession().getAttribute("warehouse");
                    req.setAttribute("warehouse",warehouse);
                    req.getSession().removeAttribute("warehouse");
                }
                req.getRequestDispatcher("/WEB-INF/jsp/warehouse/add.jsp").forward(req,resp);
            }else {
                add(req,resp);
            }
        } else if ("/delete".equals(action)) {
            delete(req,resp);
        }else if("/edit".equals(action)){
            if ("get".equals(method)){
                String id = req.getParameter("id");
                if (StringUtils.hasText(id)){
                    Warehouse warehouse = warehouseService.findById(Integer.valueOf(id));
                    if (warehouse==null){
                        req.setAttribute("error","仓库不存在");
                    }else{
                        req.setAttribute("warehouse",warehouse);
                    }
                }else{
                    req.setAttribute("error","未提供修改的仓库主键");
                }
                req.getRequestDispatcher("/WEB-INF/jsp/warehouse/edit.jsp").forward(req,resp);
            }else{
                edit(req,resp);
            }

        }
    }



    private void list(HttpServletRequest req, HttpServletResponse resp) {
        WarehouseSearchBean wsb = new WarehouseSearchBean();
        String warehouseId = req.getParameter("warehouseId");
        String warehouseName = req.getParameter("warehouseName");
        String address = req.getParameter("address");
        String areaFrom = req.getParameter("areaFrom");
        String areaTo = req.getParameter("areaTo");
        String phone = req.getParameter("phone");
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
        int iPageNo = 1;
        int iPageSize = 10;
        if (StringUtils.hasText(pageNo)){
            iPageNo = Integer.valueOf(pageNo);
        }
        if (StringUtils.hasText(pageSize)){
            iPageSize = Integer.valueOf(pageSize);
        }

        if (StringUtils.hasText(warehouseId)){
            wsb.setWarehouseId(warehouseId);
        }
        if (StringUtils.hasText(warehouseName)){
            wsb.setWarehouseName(warehouseName);
        }
        if (StringUtils.hasText(address)){
            wsb.setAddress(address);
        }
        if (StringUtils.hasText(areaFrom)){
            wsb.setAreaFrom(Integer.valueOf(areaFrom));
        }
        if (StringUtils.hasText(areaTo)){
            wsb.setAreaTo(Integer.valueOf(areaTo));
        }
        if (StringUtils.hasText(phone)){
            wsb.setPhone(phone);
        }
        PaginateInfo pi = new PaginateInfo(iPageNo,iPageSize);
        List<Warehouse> warehouseList = warehouseService.findAll(wsb,pi);
        ServletUtils.renderJson(Map.of("pi",pi,"warehouseList",warehouseList),resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String warehouseId = req.getParameter("warehouseId");
        String warehouseName = req.getParameter("warehouseName");
        String address = req.getParameter("address");
        String area = req.getParameter("area");
        String phone = req.getParameter("phone");

        Warehouse warehouse = new Warehouse();
        String error = null;
        HttpSession session = req.getSession();

        //进行后端校验
        if (!StringUtils.hasText(warehouseId)){
            error = "仓库编号不可为空";
        }
        warehouse.setWarehouseId(warehouseId);
        if (!StringUtils.hasText(warehouseName)){
            if (error==null){
                error="仓库名不可为空";
            }
        }
        warehouse.setWarehouseName(warehouseName);
        if (!StringUtils.hasText(address)){
            if (error==null){
                error = "地址不可为空";
            }
        }
        warehouse.setAddress(address);
        if (!StringUtils.hasText(area)){
            if (error==null){
                error = "仓库面积不可为空";
            }
        }
        Integer area1 = null;
        if (area!=""){
            area1 = Integer.valueOf(area);
            warehouse.setArea(area1);
        }
        if (!StringUtils.hasText(phone)){
            if (error==null){
                error = "电话号不可为空";
            }
        }
        warehouse.setPhone(phone);
        if (error!=null){
            session.setAttribute("error",error);
            session.setAttribute("warehouse",warehouse);
            resp.sendRedirect(req.getContextPath()+"/warehouse/add");
            return;
        }
        //调用业务层添加数据
        boolean success = warehouseService.add(warehouse);
        if (success){
            resp.sendRedirect(req.getContextPath()+"/warehouse/list");
        }else{
            session.setAttribute("error","添加失败,请稍后再试或联系管理员");
            session.setAttribute("warehouse",warehouse);
            resp.sendRedirect(req.getContextPath()+"/warehouse/add");
        }

    }
    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        String[] ids = req.getParameterValues("ids");
        Integer[] intIds = Stream.of(ids).map(Integer::valueOf).toArray(Integer[]::new);

        int rows = warehouseService.deleteByIds(intIds);
        ServletUtils.renderJson(Map.of("success",true,"rows",rows),resp);
    }


    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Warehouse warehouse = warehouseService.findById(Integer.valueOf(id));
        if (warehouse==null){
            resp.sendRedirect(req.getContextPath()+"/warehouse/edit");
            return;
        }

        String warehouseId = req.getParameter("warehouseId");
        String warehouseName = req.getParameter("warehouseName");
        String address = req.getParameter("address");
        String area = req.getParameter("area");
        String phone = req.getParameter("phone");
        String error = null;
        HttpSession session = req.getSession();

        //进行后端校验
        if (!StringUtils.hasText(warehouseId)){
            error = "仓库编号不可为空";
        }
        warehouse.setWarehouseId(warehouseId);
        if (!StringUtils.hasText(warehouseName)){
            if (error==null){
                error="仓库名不可为空";
            }
        }
        warehouse.setWarehouseName(warehouseName);
        if (!StringUtils.hasText(address)){
            if (error==null){
                error = "地址不可为空";
            }
        }
        warehouse.setAddress(address);
        if (!StringUtils.hasText(area)){
            if (error==null){
                error = "仓库面积不可为空";
            }
        }
        Integer area1 = null;
        if (area!=""){
            area1 = Integer.valueOf(area);
            warehouse.setArea(area1);
        }
        if (!StringUtils.hasText(phone)){
            if (error==null){
                error = "电话号不可为空";
            }
        }
        warehouse.setPhone(phone);
        if (error!=null){
            session.setAttribute("error",error);
            session.setAttribute("warehouse",warehouse);
            resp.sendRedirect(req.getContextPath()+"/warehouse/edit?id="+id);
            return;
        }
        boolean success = this.warehouseService.update(warehouse);
        if (success){
            resp.sendRedirect(req.getContextPath()+"/warehouse/list");
        }else{
            session.setAttribute("error",error);
            session.setAttribute("warehouse",warehouse);
            resp.sendRedirect(req.getContextPath()+"warehouse/edit?id="+id);
            return;
        }

    }

}
